#include "lib.h"

#include "TDBInterface.h"
#include "odbc.h"
#include "opts.h"
#include "TGame.h"
#include "tracksql.h"
#include "TriCardDefs.h"
#include "TriCardCommon.h"
#include "memory.h"
#include "badpacketmsgs.h"


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     TDBInterface::TDBInterface(TrackSQL *pTracker, TInfo *pInfo)
///
/// @param  pTracker    (in) provides connections to the database and has other useful abilities
/// @param  pInfo       (in) info about the current player session
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
TDBInterface::TDBInterface(TrackSQL *pTracker, TInfo *pInfo)
{
    m_pTracker  = pTracker;
    m_pInfo     = pInfo;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     TDBInterface::trackGetBalance( int32 *balance )
///
///  Get the current user's balance
///
/// @param  balance     (out) the current player's balance
/// 
/// @return bool8       true on success, false otherwise
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 TDBInterface::trackGetBalance( int32 *balance )
{    
    int32 result = TrackSQL::ExecFailed;
    int32 comp;
    *balance = 0;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->getBalance(ServerOpts::casinoId, m_pInfo->pid, 
                                        m_pInfo->forMoney, balance, &comp);

        m_pTracker->unlock();
    }

    return (bool8)(result == TrackSQL::NoError);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     TDBInterface::trackAdjustBalance(int32 Amount)
///
/// Change the current user's balance.
///
/// @param  Amount      (in)  the amount to add or subtract from the balance
/// 
/// @return int32       the new adjusted balance
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
int32 TDBInterface::trackAdjustBalance(int32 Amount)
{
    int32 result = TrackSQL::ExecFailed;
    int32 bal=0, comp=0;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->adjustBalance(ServerOpts::casinoId, m_pInfo->pid, 
                                           m_pInfo->forMoney, Amount, &bal, &comp);

        m_pTracker->unlock();
    }

    if (result == TrackSQL::NoError)
    {

        return bal;
    }

    return 0;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     TDBInterface::trackGetPlayerLimits(TriCardGameId eMachineId, int32* minBet, 
///                                            int32* maxBet, int32* compFactor)
///
/// Get min/max betting limit info for the indicated game.
///
/// @param  eMachineId      (in)    The machine id of the game that we want the settings for.
/// @param  minBet          (out)   lower limit for bets on the indicated game
/// @param  maxBet          (out)   upper limit for bets on the indicated game
/// @param  compFactor      (out)  
/// 
/// @return bool8           true on success, false otherwise.
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 TDBInterface::trackGetPlayerLimits(TriCardGameId eGameId, int32* minBet, 
                                         int32* maxBet, int32* compFactor)
{
    int32 result = TrackSQL::ExecFailed;

    if (m_pTracker && m_pInfo)
    {
        m_pTracker->lock();

        result = m_pTracker->getPlayerLimits(m_pInfo->pid, eGameId, 0,
                                             minBet, maxBet, compFactor);

        m_pTracker->unlock();
    }

    return (bool8)(result == TrackSQL::NoError);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     TDBInterface::trackValidateLogin()
///
/// Complete the login process for the current player (using the data in the TInfo object).
/// 
/// @return     true on success, false otherwise.
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 TDBInterface::trackValidateLogin()
{
    int32 result = TrackSQL::ExecFailed;

    if (m_pTracker)
    {
        m_pTracker->lock();

        result = m_pTracker->validateLogin(m_pInfo->login, m_pInfo->sessionId, m_pInfo->pid);

        m_pTracker->unlock();
    }

    return (bool8)(result == TrackSQL::NoError);
}



//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     void TDBInterface::trackaddBadPacket(TriCardGameId eGameId, int32 error_reason, 
///                                              char MessageType, char *msgbuf, int32 msglen)
///
/// Used for logging invalid incoming message packets.
///
/// @param  eGameId         (in) identifies which game received the bad message
/// @param  error_reason    (in) indicates what approximate problem was found
/// @param  MessageType     (in) the character identifying what type of message had the problem
/// @param  msgbuf          (in) the actual contents of the message buffer that was received
/// @param  msglen          (in) length of message buffer received
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
void
TDBInterface::trackaddBadPacket(TriCardGameId eGameId, int32 error_reason, char MessageType, char *msgbuf, int32 msglen)
{
    if (m_pTracker)
    {
		m_pTracker->lock();
		m_pTracker->addBadPacket(eGameId, 0, sBadPacketMessages[error_reason].message,
			                     sBadPacketMessages[error_reason].severity,
			                     m_pInfo->pid, MessageType, (uint8 *)msgbuf, msglen);
		m_pTracker->unlock();
	}
}